<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.ld.com/shield/tags" prefix="shield"%>
<%@ page trimDirectiveWhitespaces="true" %>
<fmt:setLocale value="zh_CN"/>
<shield:permssion type="button">
<form>
<table class="tab-list shield-table-odd"  id="popupSelect" >
    <tr>
    	
    	<c:if test="${!hasOrder }">
        <th class="w50 taCenter">
        <c:choose>
        	<c:when test="${muli}"><input type="checkbox" id="checkall"/></c:when>
        	<c:otherwise>
        		单选
        	</c:otherwise>
        </c:choose>
        </th>
        </c:if>
        <th class="w50 taCenter">序号</th>
	    <th>表名</th>
	     <th>数据量</th>
	      <th>注释</th>
    </tr>
<c:forEach var="model" items="${pageBean.resultList }" varStatus="var">
    <tr class="userSelect" data-id="${model.tableName}" data-name="${model.tableName}"  >
    	<c:if test="${!hasOrder }">
    		<td>
            <c:choose>
	        	<c:when test="${muli}"> 
	        		<input type="checkbox"  name="id" data-id="${model.tableName}" data-name="${model.tableName}" />
	        	</c:when>
	        	<c:otherwise>
	            	<input type="checkbox" onclick="userCheck(this)" name="id" data-id="${model.tableName}" data-name="${model.tableName}" />
	        	</c:otherwise>
	            </c:choose>
             </td>
       </c:if>
        <td class="taCenter">${(pageBean.currentPage -1)*pageBean.pageSize+var.count }</td>
	   	<td >${model.tableName }</td>
	   	  <td >${model.dataSize }</td>
          <td  >${model.tableComments }</td>
    </tr>
</c:forEach>
<c:if test="${empty pageBean.resultList }">
 	<!-- 无数据时展示，有数据时展示上面的列表 -->
	<tr >
        <td colspan="5" class="taCenter">暂无数据</td>
    </tr>
</c:if>

</table>
</form>
</shield:permssion>
<%@include file="/WEB-INF/view/a/include/pagination.jsp"%>
<div class="search-bt taCenter">
	<%-- 增加样式selectBtn，标明是选择按钮，dataId用于传递id，dataName用于显示名称 --%>
	<input class="infoAdd selectBtn" type="button" data-id="" data-name="" value="选择">
</div>	
<%-- muli=true 是多选，false 是单选 --%>
<c:if test="${!muli}">
  <script type="text/javascript">
		$(function(){
			$(":checkbox[name='id']").click(function(){
				 var v=$(this).data("id");
				  $(":checkbox[name='id']").each(function(){ 
				   if( $(this).data("id")!=v){
				       $(this).prop('checked', false);
				   }
				  });
			});
		})
</script>
</c:if>
<c:if test="${hasOrder }">
  <script type="text/javascript">
  var selectParams=":button.selectBtn";
  var num =parseInt('${num}');//最多选择几个
  $(function(){
	 var initDataId="";
	 var initDataName="";
	 $("#selectUserContent").find('img').each(function(index){
		 if(index==0)
		 {
		 	initDataId=$(this).data("id")
		 	initDataName= $(this).data("name");
		 }else
		 {
			 initDataId=initDataId+","+$(this).data("id")
			 initDataName=initDataName+","+ $(this).data("name");
		 }
	 });
	  $(selectParams).data("id",initDataId);
	  $(selectParams).data("name",initDataName);
	  $('tr.userSelect').click(function(){
		  var dataId = $(selectParams).data("id");
		  var dataName = $(selectParams).data("name");
		  //判断是否已经选择过数据
		  console.log(dataId);
		  if(dataId){
		  var tempDataId = ","+dataId+",";
	  	  if(tempDataId.indexOf(","+$(this).data("id")+",")!=-1){
	  	  	return false;
		  }else
		  {
		    var  data = dataId+","+$(this).data("id");
		   	var length = 	data.split(",").length;
		   	if(num!=0&&length>num)
	   		{
		   		ShieldJS.alert("title","最多选择"+num+"个！");
		   		return false;
	   		}
			$(selectParams).data("id",data);
		  }
		  }else
		  {
			  $(selectParams).data("id",$(this).data("id"));
		  }
		  if(dataName){
			  $(selectParams).data("name",dataName+","+$(this).data("name"));
		  }else{
		  		$(selectParams).data("name",$(this).data("name"));
		  }
		  $("#selectUserContent").append("<tr><td>"+$(this).data("name")+"</td><td style='text-align:center'><a><img src='${ctxPath}/images/icon/delete.png' data-id='"+$(this).data("id")+"' data-name='"+$(this).data("name")+"' onClick='remove(this)' class='c_p' title='删除'></a></td></tr>");
	  });
	  
  })
  function remove(obj)
  {
	  var dataOldId = ","+$(selectParams).data("id")+",";
	  var dataOldName = ","+$(selectParams).data("name")+",";
	  dataOldId =  dataOldId.replace(","+$(obj).data('id')+",",",");
	  if(dataOldId.length==1)
	  {
		  dataOldId=dataOldId.substring(0,dataOldId.length-1);
	  }else
	  {
		  dataOldId=dataOldId.substring(1,dataOldId.length-1);
	  }
	  dataOldName =   dataOldName.replace(","+$(obj).data('name')+",",",");
	  if(dataOldName.length==1)
	  {
		  dataOldName=dataOldName.substring(0,dataOldName.length-1);
	  }else
	  {
		  dataOldName=dataOldName.substring(1,dataOldName.length-1);
	  }
	  var dataId = $(selectParams).data("id",dataOldId);
	  var dataName = $(selectParams).data("name",dataOldName);
	 $(obj).parents('tr').remove();
  }
  </script>
</c:if>